package cn.javazd.demo;

import java.util.Arrays;

public class SortTest {

	private static int sorts[] = { 3, 1, 5, 7, 2, 4, 9, 6, 10, 8 };

	public static void main(String[] args) {
		System.out.println(Arrays.toString(sorts));
//		insertSort();
		bubbleSort();
	}

	/**
	 * 插入排序
	 */
	public static void insertSort() {
		for (int i = 1; i < sorts.length; i++) {
			int temp = sorts[i];
			int j;
			for (j = i - 1; j >= 0 && sorts[j] > temp; j--) {
				sorts[j + 1] = sorts[j];
			}
			sorts[j + 1] = temp;
		}
		System.out.println(Arrays.toString(sorts));
	}

	/**
	 * 冒泡排序
	 */
	public static void bubbleSort() {
		for (int i = 0; i < sorts.length - 1; i++) {
			for (int j = 0; j < sorts.length - i - 1; j++) {
				if (sorts[j] > sorts[j + 1]) {
					int temp = sorts[j];
					sorts[j] = sorts[j + 1];
					sorts[j + 1] = temp;
				}
			}
		}
		System.out.println(Arrays.toString(sorts));
	}
}
